package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateTagRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteTagRequest;
import com.haohan.jingyuan.customer.dto.req.QueryTagListRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateTagRequest;
import com.haohan.jingyuan.customer.dto.resp.TagInfoResp;
import com.haohan.jingyuan.customer.dto.resp.TagListResp;
import com.haohan.jingyuan.customer.service.CustomerTagService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户标签控制器
 *
 * @ClassName CustomerTagController
 * @Description 客户标签相关接口控制器
 * @Author hexijun
 * @Time 2025/10/30 10:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerTagController {

    /**
     * 客户标签服务
     */
    private final CustomerTagService customerTagService;

    /**
     * 创建标签
     *
     * @param request 创建标签请求
     * @return 标签信息
     */
    @PostMapping("/customer/tag/create")
    @NeedLogin
    public GwResponse<TagInfoResp> createTag(@Valid @RequestBody GwRequest<CreateTagRequest> request) {
        log.info("CustomerTagController.createTag BEGIN ==> {}", JSONUtil.toJsonStr(request));

        TagInfoResp respData = customerTagService.createTag(request.getRequestData());
        GwResponse<TagInfoResp> response = GwResponse.success("创建标签成功", respData);

        log.info("CustomerTagController.createTag END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 更新标签
     *
     * @param request 更新标签请求
     * @return 标签信息
     */
    @PostMapping("/customer/tag/update")
    @NeedLogin
    public GwResponse<TagInfoResp> updateTag(@Valid @RequestBody GwRequest<UpdateTagRequest> request) {
        log.info("CustomerTagController.updateTag BEGIN ==> {}", JSONUtil.toJsonStr(request));

        TagInfoResp respData = customerTagService.updateTag(request.getRequestData());
        GwResponse<TagInfoResp> response = GwResponse.success("更新标签成功", respData);

        log.info("CustomerTagController.updateTag END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除标签
     *
     * @param request 删除标签请求
     * @return 删除结果
     */
    @PostMapping("/customer/tag/delete")
    @NeedLogin
    public GwResponse<String> deleteTag(@Valid @RequestBody GwRequest<DeleteTagRequest> request) {
        log.info("CustomerTagController.deleteTag BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerTagService.deleteTag(request.getRequestData());
        GwResponse<String> response = success ? GwResponse.success("删除标签成功", null) : GwResponse.error("删除标签失败");

        log.info("CustomerTagController.deleteTag END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询标签列表
     *
     * @param request 查询标签列表请求
     * @return 标签列表
     */
    @PostMapping("/customer/tag/list")
    @NeedLogin
    public GwResponse<TagListResp> queryTagList(@Valid @RequestBody GwRequest<QueryTagListRequest> request) {
        log.info("CustomerTagController.queryTagList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        TagListResp respData = customerTagService.queryTagList(request.getRequestData());
        GwResponse<TagListResp> response = GwResponse.success("查询标签列表成功", respData);

        log.info("CustomerTagController.queryTagList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}


